package tw.fragmental.barriga.domain;

import java.util.HashMap;
import java.util.Map;

public class Billable {

	private String name = null;
	private int numberOfParts = 1;
	private long amount = 0l;
	private PayDay payDay = PayDay.NOT_SET;
	private Map<Schedule, Action> actions = new HashMap<Schedule, Action>();

	public Billable(String name) {
		this.name = name;
	}

	public int getNumberOfParts() {
		return this.numberOfParts;
	}

	public long getAmount() {
		return this.amount;
	}

	public void setNumberOfParts(int newNumberOfParts) {
		this.numberOfParts = newNumberOfParts;
	}

	public void setAmount(int newAmount) {
		this.amount = newAmount;
	}

	public void setPayDay(PayDay newPayDay) {
		this.payDay = newPayDay;
	}

	public String getName() {
		return this.name;
	}

	public PayDay getPayDay() {
		return this.payDay;
	}

	public void onPayment(Schedule condition, Action action) {
		actions.put(condition, action);
	}

	public void pay(PayDay when, int howMuch) {
		if(when.isBefore(this.payDay)){
			actionPerformedWhenPaymentIsOnTime().execute(this);	
		}else{
			actionPerformedWhenThePaymentIsLate().execute(this);
		}
	}

	private Action actionPerformedWhenThePaymentIsLate() {
		return this.actions.get(Schedule.LATE);
	}

	private Action actionPerformedWhenPaymentIsOnTime() {
		return this.actions.get(Schedule.ON_TIME);
	}

}